import { devicePortFieldDataRecordExportApi } from "@/api/devices/Device"

let datas:{[key:string] : any} = {}


export async function doExport(devices:{id:number, type:number, portModel:number}[], portModels:{[key:string] : number[][]}) {
    const keys:number[][] = []
    for(let i = 0; i < devices.length; i ++) {
        const device = devices[i]
        const portModel = portModels[device.portModel]
        portModel.forEach((port, index) => {
            port.forEach(p => {
                keys.push([device.id, index, p])
            })
        })
    }

    
    const deviceIdToRows:{[key:string] : any[]} = {}

    for(let i = 0; i < keys.length; i ++) {
        //const rows = await step([1001030, 0, 2])
        //console.log(rows)
        const rows = await step(keys[i])
        if(deviceIdToRows[keys[i][0]]) {
            deviceIdToRows[keys[i][0]].push(...rows)
        }else {
            deviceIdToRows[keys[i][0]] = rows
        }
    }

    
}

async function step(key:number[]) {
    let pageNo = 1
    const pageSize = 3
    let rows:any[] = []

    while(true) {
        const res = await devicePortFieldDataRecordExportApi(key[0], key[1], key[2], {pageNo, pageSize})
        if(res.data) {
            rows.push()
            rows = rows.concat(res.data)
            if(res.data.length == pageSize) {
                pageNo ++ 
            }else {
                break
            }
        }else {
            break
        }
    }

    return rows
}